<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

    <base href="<%=basePath%>">
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<link rel="stylesheet" type="text/css" href="<%=basePath%>/easyui/themes/default/easyui.css" />
	<link rel="stylesheet" type="text/css" href="<%=basePath%>/easyui/themes/icon.css" />
	<link rel="stylesheet" type="text/css" href="<%=basePath%>/css/login.css" />
	<title>Login Page</title> 
</head>

<body>

	<div id="login">
		<p>管理员帐号：<input type="text" id="userName" class="textbox"></p>
		<p>管理员密码：<input type="password" id="password" class="textbox"></p>
		<p>验证码：<input type="text" id="verificationCode" class="textbox" style="wight:30px"> 
			<img id="checkcode" src="servlet/checkcode"  onclick="reloadVerifyCode(this);"> 
		</p>
	</div>

	<div id="btn">
		<a href="javascript:void(0)" class="easyui-linkbutton">登录</a>
	</div>

	<script type="text/javascript" src="<%=basePath%>/easyui/jquery.min.js"></script>
	<script type="text/javascript" src="<%=basePath%>/easyui/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="<%=basePath%>/easyui/locale/easyui-lang-zh_CN.js" ></script>
	<script type="text/javascript" >
		if(self!=top) {
			top.location=self.location;
		}  
		function reloadVerifyCode(obj){  
			var timenow = new Date().getTime();                          
			obj.src="servlet/checkcode?d= " + timenow;  
		}

		$(document).keyup(function(event) {
			if (event.keyCode == 13) {
				$("#btn a").trigger("click");
			}
		});

		$(function () {
			//登录界面
			$('#login').dialog({
				title : '登录系统',
				width : 320,
				height : 260,
				modal : true,
				iconCls : 'icon-login',
				buttons : '#btn'
			});
			
			//验证码图片提示
			$('#checkcode').tooltip({
				content : '<strong>看不清，点击换一张</strong>',
				position : 'bottom'
			});
			
			//管理员帐号验证
			$('#userName').validatebox({
				required : true,
				missingMessage : '请输入管理员帐号',
				invalidMessage : '管理员帐号不得为空',
			});
			
			//管理员密码验证
			$('#password').validatebox({
				required : true,
				validType : 'length[3,15]',
				missingMessage : '请输入管理员密码',
				invalidMessage : '管理员密码应在3 - 15位之间',
			});
			
			//验证码验证
			$('#verificationCode').validatebox({
				//required : true,
				
				//missingMessage : '请输入验证码'
			});
			
			//加载时判断验证
			if (!$('#userName').validatebox('isValid')) {
				$('#userName').focus();
			} else if (!$('#password').validatebox('isValid')) {
				$('#password').focus();
			} else if (!$('#verificationCode').validatebox('isValid')) {
				$('#verificationCode').focus();
			}
			
			//单击登录
			$('#btn a').click(function () {
				if (!$('#userName').validatebox('isValid')) {
					$('#userName').focus();
				} else if (!$('#password').validatebox('isValid')) {
					$('#password').focus();
				} else if (!$('#verificationCode').validatebox('isValid')) {
					$('#verificationCode').focus();
				} else {
					$.ajax({
						url : 'loginAction!checkLogin',
						type : 'post',
						data : {
							userName : $('#userName').val(),
							password : $('#password').val(),
							verificationCode : $('#verificationCode').val(),
						},
						beforeSend : function () {
							$.messager.progress({
								text : '正在登录中...'
							});
						},
						success : function (data, response, status) {
							$.messager.progress('close');
							
							if (data == 0) {
								$.messager.alert('登录失败！', '验证码错误！', 'error', function () {
									$('#verificationCode').select();
								});
							} else if (data == 1){
								$.messager.alert('登录失败！', '用户名或密码错误！', 'error', function () {
									$('#password').select();
								});
							} else if (data == 2){
								location.href = 'jsp/index.jsp';
							} else if(data == 3){
								$('#login').dialog('close');
								$.messager.alert('登录失败！', '用户名或密码错误！', 'error');
							} else if(data == 4){
								$.messager.alert('登录失败！', '该用户不存在该角色,请重新选择角色！', 'error', function () {
									$('#selectUserRole').select();
								});
							}
						}
					});
				}
			});
			
		});

	</script>
</body>
</html>